package com.lebang.controller.api.earned;

import com.alibaba.fastjson.JSONObject;
import com.lebang.aop.Sessionid;
import com.lebang.common.MemberLock;
import com.lebang.common.Require;
import com.lebang.common.ValidCode;
import com.lebang.component.RedisClient;
import com.lebang.domain.AppEarnNewsConfig;
import com.lebang.entity.RequestHeaders;
import com.lebang.feign.JokeEarnedFeign;
import com.lebang.model.RequestUserInfo;
import com.lebang.model.back.Result;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.LuckDrawService;
import com.lebang.service.MemberNewTaskService;
import com.lebang.service.PointService;
import com.lebang.service.earn.AppEarnLookService;
import com.lebang.service.sys.DictService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: hwq
 * @Date: 2020/3/10 9:58
 * @Version 1.0
 */
@RestController
@RequestMapping("/app/joke")
public class JokeEarnApi {
    @Autowired
    private AppEarnLookService appEarnLookService;
    private static final String XWZ = "joke";
    private static final String KEY = "lock:jokeearn:";
    @Autowired
    private PointService pointService;
    @Autowired
    private LuckDrawService luckDrawService;
    @Autowired
    private MemberNewTaskService newTaskService;
    @Autowired
    private JokeEarnedFeign jokeEarnedFeign;

    @Autowired
    private DictService dictService;

    /**
     * 开关
     *
     * @param request
     * @return
     */
//    @Require
    @Sessionid
    @PostMapping("/enable")
    public JsonData enable(HttpServletRequest request, String key) {

        if (dictService.selectByKeyName("open_new_joke").getKeyvalue().equals("1")) {
            RequestUserInfo userInfo = new RequestUserInfo(request);
            return jokeEarnedFeign.enable(userInfo, key);
        } else {
            AppEarnNewsConfig config = appEarnLookService.selectByKey(XWZ);
            if (config == null) {
                return Callback.fail("fail", "操作失败", null);
            }
            if (config.getEnable() > 0) {
                return Callback.success("success", "操作成功", null);
            } else {
                return Callback.fail("fail", "操作失败", null);
            }
        }

    }

    /**
     * 任务列表
     *
     * @param request
     * @return
     */
    @Require
    @Sessionid
    @PostMapping("/list")
    public JsonData list(HttpServletRequest request, String key) {

            RequestUserInfo userInfo = new RequestUserInfo(request);
            return jokeEarnedFeign.list(userInfo, key);

    }

    /**
     * 领取任务
     *
     * @param request
     * @return
     */
    @Require
    @Sessionid
    @PostMapping("/pull")
    @MemberLock
    public JsonData pull(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        try {

                JsonData jsonData = jokeEarnedFeign.pull(userInfo);
                if (jsonData.isSuccess()) {
                    //成功
                    JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(jsonData.getData()));
                    object.put("ad", luckDrawService.selectAdByBrand(userInfo.getAppStore(), Integer.parseInt(userInfo.getVersion())));
                    jsonData.setData(object);
                } else {
                    return jsonData;
                }

                return jsonData;


        } catch (Exception e) {
            e.printStackTrace();
            return Callback.fail("205","服务器异常" ,e.getMessage());
        }


    }

    /**
     * 提交任务
     *
     * @param request
     * @return
     */
    @Require
    @Sessionid
    @PostMapping("/push")
    @ValidCode
    @MemberLock
    public JsonData push(HttpServletRequest request, Long id) {

            RequestHeaders header = new RequestHeaders(request);
            RequestUserInfo userInfo = new RequestUserInfo(request);
            Result<Integer> result = jokeEarnedFeign.push(userInfo, id);
            if (result.isSuccess()) {
                //成功
                pointService.addPoint(header.getUserid(), result.getData(), TaskCategory.JOKE_EARN, header, "");
                newTaskService.pushRecord(TaskCategory.JOKE_EARN, header.getUserid());
            } else {
                return Callback.fail(result.getStatus(), result.getMessage());
            }
            return Callback.success(result.getStatus(), result.getMessage(), result.getData());


    }

    @Require
    @Sessionid
    @PostMapping("/double")
    @ValidCode
    @MemberLock
    public JsonData doubleCount(HttpServletRequest request, Long id) {


            RequestHeaders header = new RequestHeaders(request);
            RequestUserInfo userInfo = new RequestUserInfo(request);
            Result<Integer> result = jokeEarnedFeign.doubleCount(userInfo, id);
            if (result.isSuccess()) {
                //成功
                pointService.addPoint(header.getUserid(), result.getData(), TaskCategory.JOKE_EARN, header, "奖励翻倍");
            } else {
                return Callback.fail(result.getStatus(), result.getMessage());
            }
            return Callback.success(result.getStatus(), result.getMessage(), result.getData());

    }
}
